package kz.kstu.aecdemo.app.delegate.impl;

import kz.kstu.aec.model.AuthenticationData;
import kz.kstu.aec.model.Point2D;
import kz.kstu.aecdemo.app.delegate.api.AccountDelegate;
import kz.kstu.aecdemo.app.util.locator.ServiceLocator;
import kz.kstu.aecdemo.app.util.locator.factory.ServiceLocatorFactory;
import kz.kstu.aecdemo.business.entity.security.ECAccount;
import kz.kstu.aecdemo.facade.ejb.api.AccountFacade;

import javax.security.auth.login.AccountException;
import javax.security.auth.login.AccountNotFoundException;
import javax.security.auth.login.LoginException;
import java.math.BigInteger;

/**
 * @author Filipp_Stankevich
 *         created: 08.09.11 14:35
 */
public class AccountDelegateImpl implements AccountDelegate {

    private AccountFacade getAccountFacade(boolean isOnline) {
        ServiceLocator serviceLocator = ServiceLocatorFactory.getServiceLocator(isOnline);
        return serviceLocator.getService(AccountFacade.SERVICE_NAME, AccountFacade.class);
    }

    @Override
    public ECAccount login(AuthenticationData authenticationData) throws LoginException {
        return getAccountFacade(false).login(authenticationData);
    }

    @Override
    public BigInteger getAuthenticationNumber(String login) throws AccountNotFoundException {
        return getAccountFacade(false).getAuthenticationNumber(login);
    }

    @Override
    public void register(String login, Point2D publicKey) throws AccountException{
         getAccountFacade(false).register(login, publicKey);
    }

    @Override
    public void changePassword(String login, Point2D publicKey) throws AccountNotFoundException {
        getAccountFacade(false).changePassword(login, publicKey);
    }

    @Override
    public int getUserCount() {
         return getAccountFacade(false).getUserCount();
    }
}
